const router = require('../../router');
const url = require('url');

const {
  pagination,
  isEmpty,
  isArray,
  PWD_REG,
  NAME_REG,
  EMAIL_REG,
  PHONE_REG
} = require('../../../utils');

const {
  // mongoose,
  cloud_register,
  cloud_login,
  cloud_change,
  signToken
} = require('../../../lib');

// const Schema = mongoose.Schema;

router.post('/register', async function (req, res) {
  const params = req.body;

  console.log('params', params);

  let flag = true,
    err = [];

  const {
    name,
    tfs,
    email,
    phone,
    upwd
  } = params;

  flag = flag && PWD_REG.test(upwd) &&
    EMAIL_REG.test(email) &&
    PHONE_REG.test(phone);

  if (!PWD_REG.test(upwd)) err.push('密码不符合规范');

  if (!EMAIL_REG.test(email)) err.push('邮箱填写不符合规范');

  if (!PHONE_REG.test(phone)) err.push('手机号码填写不符合规范');

  // const registerSchema = new Schema({
  //   name: String,
  //   tfs: String,
  //   email: String,
  //   phone: String,
  //   upwd: String
  // });

  // const Register = mongoose.model('Register', registerSchema);


  if (flag) {
    // const register = new Register({
    //   name,
    //   tfs,
    //   email,
    //   phone,
    //   upwd
    // });



    // register.save().then((result)=>{
    //     console.log("成功的回调", result);

    //     res.json({
    //       code: "0",
    //       data: {},
    //       msg: '成功',
    //       success: true
    //     });
    // },(err)=>{
    //     console.log("失败的回调", err);

    //     res.json({
    //       code: "-1",
    //       data: {
    //         err: err
    //       },
    //       msg: '失败',
    //       success: false
    //     });
    // });

    let r = await cloud_register('imagepic', 'auth', {
      name,
      tfs,
      email,
      phone,
      upwd
    });

    if (r.flag) {
      res.json({
        code: "0",
        data: {},
        msg: '成功',
        success: true
      });
    } else {
      res.json({
        code: "-1",
        data: {
          err: r.msg
        },
        msg: '失败',
        success: false
      });
    }

  } else {
    res.json({
      code: "-1",
      data: {
        err: err.join(',')
      },
      msg: '失败',
      success: false
    })
  }
});

router.post('/login', async function (req, res) {
  const params = req.body;

  console.log('params', params);

  let flag = true,
    err = [];

  const {
    phone,
    upwd
  } = params;

  flag = flag && PWD_REG.test(upwd) &&
    PHONE_REG.test(phone);

  if (!PWD_REG.test(upwd)) err.push('密码不符合规范');

  if (!PHONE_REG.test(phone)) err.push('手机号码填写不符合规范');

  // const registerSchema = new Schema({
  //   name: String,
  //   tfs: String,
  //   email: String,
  //   phone: String,
  //   upwd: String
  // });

  // const Register = mongoose.model('Register', registerSchema);


  if (flag) {
    // const register = new Register({
    //   name,
    //   tfs,
    //   email,
    //   phone,
    //   upwd
    // });



    // register.save().then((result)=>{
    //     console.log("成功的回调", result);

    //     res.json({
    //       code: "0",
    //       data: {},
    //       msg: '成功',
    //       success: true
    //     });
    // },(err)=>{
    //     console.log("失败的回调", err);

    //     res.json({
    //       code: "-1",
    //       data: {
    //         err: err
    //       },
    //       msg: '失败',
    //       success: false
    //     });
    // });

    let r = await cloud_login('imagepic', 'auth', {
      phone,
      upwd
    });

    if (r.flag) {
      const token = signToken({
        phone,
        upwd
      }, 'imagepic');
      // console.log('token', token)
      res.json({
        code: "0",
        data: {
          token: token
        },
        msg: '成功',
        success: true
      });
    } else {
      res.json({
        code: "-1",
        data: {
          err: r.msg
        },
        msg: '失败',
        success: false
      });
    }

  } else {
    res.json({
      code: "-1",
      data: {
        err: err.join(',')
      },
      msg: '失败',
      success: false
    })
  }
});

router.post('/change', async function (req, res) {
  const params = req.body;

  console.log('params', params);

  let flag = true,
    err = [];

  const {
    phone,
    opwd,
    npwd
  } = params;

  flag = flag && PWD_REG.test(opwd) &&
    PWD_REG.test(npwd) &&
    PHONE_REG.test(phone);

  if (!PWD_REG.test(opwd)) err.push('旧密码不符合规范');

  if (!PWD_REG.test(npwd)) err.push('新密码不符合规范');

  if (!PHONE_REG.test(phone)) err.push('手机号码填写不符合规范');

  if (flag) {
    let r = await cloud_login('imagepic', 'auth', {
      phone: phone,
      upwd: opwd
    });

    if (r.flag) {
      const changeResponse = await cloud_change('imagepic', 'auth', {
        phone: phone,
        upwd: npwd
      });

      if(changeResponse.flag) {
        res.json({
          code: "0",
          data: {},
          msg: '成功',
          success: true
        });
      } else {
        res.json({
          code: "-1",
          data: {
            err: changeResponse.msg
          },
          msg: '失败',
          success: false
        });
      }
    } else {
      res.json({
        code: "-1",
        data: {
          err: r.msg
        },
        msg: '失败',
        success: false
      });
    }

  } else {
    res.json({
      code: "-1",
      data: {
        err: err.join(',')
      },
      msg: '失败',
      success: false
    })
  }
})

module.exports = router;